package shuzi.activity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import shuzi.adapter.BrandPhoneAdapter;
import shuzi.data.Database;
import shuzi.model.BrandPhone;
import shuzi.util.SnComparator;
import shuzi.util.Webserice;
import shuzi.widget.SortChoiceLayout;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.PauseOnScrollListener;
import com.shuzi.offer.R;

/**
 * @author lung 下午3:40:39 2013
 */
public class PhoneInfoActivity extends Activity implements OnClickListener {

	/** 刷新界面的handler */
	private Handler handler = null;
	/** 全部数据 */
	private List<BrandPhone> listAll = null;
	/** 滚动 */
	private ProgressBar progressBar = null;
	/** 列表 */
	private ListView listView = null;
	/** 适配器 */
	private BrandPhoneAdapter adapter = null;
	/** 悬浮框 */
	private PopupWindow popup = null;
	/** 选择框 */
	private SortChoiceLayout sort = null;
	/** 选择按钮 */
	private Button choice = null;
	/** 订单按钮 */
	private Button order = null;
	/**    */
	private Intent intent = null;
	/**标题    */
	private TextView titleText = null;
	private ImageLoader imageLoader = ImageLoader.getInstance();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.phone_info);
		initHandler();
		initLayout();
		initPopWindow();
		initDada();

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		if (popup != null && popup.isShowing()) {
			popup.dismiss();
		}
		return super.onTouchEvent(event);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (popup != null && popup.isShowing()) {
				popup.dismiss();
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * 初始化handler
	 */
	@SuppressLint("HandlerLeak")
	private void initHandler() {
		handler = new Handler() {
			@SuppressWarnings("unchecked")
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) {
				case Database.HANDLER_REFRESH_LISTVIEW:
					listAll = (List<BrandPhone>) msg.obj;
					progressBar.setVisibility(View.GONE);
					adapter = new BrandPhoneAdapter(PhoneInfoActivity.this,
							listAll);
					listView.setAdapter(adapter);
					/*
					 * Toast.makeText(PhoneInfoActivity.this,
					 * "listAll"+listAll.size(), Toast.LENGTH_LONG).show();
					 */
					break;
				case Database.HANDLER_REFRESH_ERROR:
					progressBar.setVisibility(View.GONE);
					Toast.makeText(PhoneInfoActivity.this, "发生错误",
							Toast.LENGTH_LONG).show();
					break;
				case Database.HANDLER_REFRESH_NODATA:
					progressBar.setVisibility(View.GONE);
					Toast.makeText(PhoneInfoActivity.this, "暂无数据",
							Toast.LENGTH_LONG).show();
					break;

				default:
					break;
				}
			}
		};

	}

	/**
	 * 初始化布局
	 */
	private void initLayout() {
		listView = (ListView) findViewById(R.id.phoneListview);
		progressBar = (ProgressBar) findViewById(R.id.progressBar);
		order = (Button) findViewById(R.id.order);
		choice = (Button) findViewById(R.id.choice);
		titleText = (TextView) findViewById(R.id.title);
		listView.setCacheColorHint(0);
		listView.setOnScrollListener(new PauseOnScrollListener(imageLoader, false, true));

		order.setOnClickListener(this);
		choice.setOnClickListener(this);

		listView.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (popup != null && popup.isShowing()) {
					popup.dismiss();
					return true;
				}
				return false;
			}
		});
	}

	/**
	 * 初始化数据
	 */
	private void initDada() {
		final boolean isRecommend = getIntent().getBooleanExtra(
				Database.IS_RECOMMEND, false);
		intent = getIntent();
		titleText.setText(intent.getStringExtra(Database.name));
		
		listAll = new ArrayList<BrandPhone>();
		

		new Thread(new Runnable() {

			@Override
			public void run() {
				if (!isRecommend) {
					Webserice.getPhoneList(intent.getStringExtra(Database.brandId), intent.getStringExtra(Database.name), handler,
							Database.HANDLER_REFRESH_LISTVIEW);
				} else {
					Webserice.getRecmendData(handler,
							Database.HANDLER_REFRESH_LISTVIEW);
				}

			}
		}).start();

	}
	
	/**
	 * 对sn排序
	 * 
	 * @param oldlist
	 */
	private void sortList(List<BrandPhone> oldlist,int type) {
		if(oldlist.size()==0||oldlist==null){
			return;
		}
		// 创建比较器对象
		SnComparator comp = new SnComparator(type);
		// 调用排序方法
		Collections.sort(oldlist, comp);
		adapter.notifyDataSetChanged();
	}

	/**
	 * 初始化悬浮框
	 */
	private void initPopWindow() {
		sort = new SortChoiceLayout(this) {
			@Override
			public void onClickId(int id) {
				super.onClickId(id);
				if (popup != null && popup.isShowing()) {
					popup.dismiss();
				}
				switch (id) {
				case SortChoiceLayout.HIGH_SORT_ID:
					sortList(listAll,0);
					choice.setText("价高");
					break;
				case SortChoiceLayout.LOW_SORT_ID:
					sortList(listAll,1);
					choice.setText("价低");
					break;
				case SortChoiceLayout.DATE_SORT_ID:
					sortList(listAll,2);
					choice.setText("日期");
					break;

				default:
					break;
				}
			}
		};
		popup = new PopupWindow(sort, -2, -2);
		popup.setContentView(sort);
		// popup.setOutsideTouchable(true);
		popup.setBackgroundDrawable(new BitmapDrawable());

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.choice:
			if (!popup.isShowing()) {
				popup.showAsDropDown(choice, 0, 5);
			} else {
				popup.dismiss();
			}

			break;

		case R.id.order:
			Intent intent = new Intent();
			intent.setClass(PhoneInfoActivity.this, OrderActivity.class);
			startActivity(intent);
			break;

		default:
			break;
		}

	}
}
